package top.tangyh.lamp.care.mapper;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import top.tangyh.basic.base.mapper.SuperMapper;
import top.tangyh.lamp.care.entity.BCareContent;
import org.springframework.stereotype.Repository;
import top.tangyh.lamp.common.utils.YDateUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * 护理记录内容表
 * </p>
 *
 * @author zqb
 * @date 2024-08-02 10:21:07
 * @create [2024-08-02 10:21:07] [zqb] [代码生成器生成]
 */
@Repository
public interface BCareContentMapper extends SuperMapper<BCareContent> {


    default boolean checkByTimeAndPatientIdentAndRecordBy(String patientIdent, LocalDateTime recordTime, String recordBy){
        Date start = YDateUtils.stringToDate(YDateUtils.format(new Date(YDateUtils.getTimestampOfDateTime(recordTime)),YDateUtils.DATE_TIME_MINUTE_PATTERN)+":00",YDateUtils.DATE_TIME_PATTERN);
        Date end = YDateUtils.stringToDate(YDateUtils.format(new Date(YDateUtils.getTimestampOfDateTime(recordTime)),YDateUtils.DATE_TIME_MINUTE_PATTERN)+":59",YDateUtils.DATE_TIME_PATTERN);
        LambdaQueryWrapper<BCareContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BCareContent::getPatientIdent,patientIdent)
                .eq(BCareContent::getRecordBy,recordBy)
                .ge(BCareContent::getRecordTime,start)
                .le(BCareContent::getRecordTime,end);
        List<BCareContent> olds = selectList(queryWrapper);
        if(CollectionUtil.isNotEmpty(olds)){
            return true;
        }
        else{
            return false;
        }
    }

    default void deleteOlds(Long recordTime,String patientIdent,String operator,String account,String userId){
        LambdaQueryWrapper<BCareContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BCareContent::getPatientIdent,patientIdent)
                .eq(BCareContent::getRecordTime,new Date(recordTime)).eq(BCareContent::getRecordBy,userId);
        delete(queryWrapper);
    }
}


